package internal

import (
	"encoding/json"
	"github.com/starmotioncf/common/filter"
	"github.com/starmotioncf/common/region"
	"github.com/starmotioncf/soutchatinternal/constant"
	"github.com/starmotioncf/soutchatpkg/jwt"

	"github.com/gin-gonic/gin"
)

func GetUidFromCtx(ctx *gin.Context) uint64 {
	tp, ok := ctx.Value(constant.CtxToken).(*jwt.TokenClaims)
	if ok {
		return tp.Uid
	}
	return 0
}

func GetRegionFromCtx(ctx *gin.Context) region.Region {
	tp, ok := ctx.Value(constant.CtxToken).(*jwt.TokenClaims)
	if ok {
		return tp.Region
	}
	return 0
}

func GetTokenPayloadCtx(ctx *gin.Context) *jwt.TokenClaims {
	tp, ok := ctx.Value(constant.CtxToken).(*jwt.TokenClaims)
	if ok {
		return tp
	}
	return nil
}

func GetFilterEntityFromCtx(ctx *gin.Context) filter.Entity {
	tp, ok := ctx.Value(constant.CtxFilterEntity).(filter.Entity)
	if ok {
		return tp
	}
	return nil
}

func GetLangFromCtx(ctx *gin.Context) string {
	return ctx.Request.Header.Get("X-Lang")
}

func GetMacFromCtx(ctx *gin.Context) string {
	return ctx.Request.Header.Get(constant.HeaderXMac)
}

func ToJson(d any) string {
	_b, _ := json.Marshal(d)
	return string(_b)
}
